extends CharacterBody2D

var move_speed = 40.0
var hp = 100

# Attack
var iceSpear = preload("res://player/attack/ice_spear.tscn")
@onready var iceSpearTimer = $attack/iceSpearTimer
@onready var iceSpearAttackTimer = $attack/iceSpearAttackTimer

var icespear_ammo = 0
var icespear_baseammo = 1
var icespear_attackspeed = 1.5
var icespear_level = 1

var enemy_close = []


@onready var playerSprite = $playerSprite
@onready var animation = $AnimationPlayer

func _ready():
	attack()

func _process(_delta):
	movement()

func movement(): 
	var x_mov = Input.get_action_strength("ui_right") - Input.get_action_strength("ui_left")
	var y_mov = Input.get_action_strength("ui_down") - Input.get_action_strength("ui_up")
	var mov = Vector2(x_mov, y_mov)
	self.velocity = mov.normalized() * move_speed
	if mov.x > 0:
		playerSprite.flip_h = true
	elif mov.x < 0:
		playerSprite.flip_h = false
	
	if mov != Vector2.ZERO:
		animation.play("walk")
	else:
		animation.pause()
	
	move_and_slide()

func attack():
	if icespear_level > 0:
		iceSpearTimer.wait_time = icespear_attackspeed
		if iceSpearAttackTimer.is_stopped():
			iceSpearAttackTimer.start()
			
func _on_hurtbox_hurt(damage: Variant) -> void:
	hp -= damage
	print(hp)
